package dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import util.HibernateSessionFactory;
import entity.Users;
import entity.Candidate;

public class UsersDao{
	/**
	 * 人员信息录入
	 * @param users
	 */
	public void addUsers(Users users){
		users.setCreatetime(new java.util.Date());//创建时间	
		Session session = HibernateSessionFactory.getSession();//session实例     
        session.beginTransaction();  //开启事务
       
        users.setIsLock((byte) 0);
        System.out.println(users);
        session.save(users);//保存用户
        session.getTransaction().commit(); //提交事务 
        HibernateSessionFactory.closeSession();
	}
	/**
	 * 查找人员是否存在
	 */
	public List<String> exists(Users user){
		Session session = HibernateSessionFactory.getSession();
		
        Query query = session.createQuery("select username from Users where id=:id and password=:password");
        
        query.setParameter("id", user.getId());
        query.setParameter("password", user.getPassword());

		List<String> username_list = query.list();
        
//        if(query.list().isEmpty()){
//
//        	return false;//不存在
//        }
//		return true;//存在

		return username_list;
	}

	//查询某一身份证的用户是否存在
	public boolean existsIdCard(Users user){
		Session session = HibernateSessionFactory.getSession();
        Query query = session.createQuery("select id from Users where idCard=:idCard");
        query.setParameter("idCard", user.getIdCard());
        if(query.list().isEmpty()){
        	
        	return false;//不存在
        }
		return true;//存在
	}
	/**
	 * 根据id加载人员信息
	 */
	public Users loadUsers(Long id){
		Session session = HibernateSessionFactory.getSession();        		
		Users u = (Users)session.get(Users.class, id);//加载指定用户名的Users对象
		return u;
	}
	/**
	 * 人员信息查看 
	 */
	public List<Users> listUser(){
		Session session = HibernateSessionFactory.getSession();	
		Query query = session.createSQLQuery("select * from staff where Work=:isLock order by createtime").addEntity(Users.class);
		query.setParameter("isLock", 0);
		List<Users> list = query.list();
		HibernateSessionFactory.closeSession();
		return list;		
	}
	
	/**
	 * 人员信息修改
	 */
	public void update(Users user){	

		Session session = HibernateSessionFactory.getSession();
		session.beginTransaction();
		session.update(user);
		session.getTransaction().commit();
		HibernateSessionFactory.closeSession();		
	}
	/**
	 * 人员信息删除
	 */
	public void deleteUsers(Users user){
		System.out.println(user.getId());
		System.out.println(user.getIsLock());
		Session session = HibernateSessionFactory.getSession();
		session.beginTransaction();
		session.update(user);
		session.getTransaction().commit();
		HibernateSessionFactory.closeSession();
	}

	public void dao_addCandidate(Candidate candidate){
		//users.setCreatetime(new java.util.Date());//创建时间
		Session session = HibernateSessionFactory.getSession();//session实例
		session.beginTransaction();  //开启事务
		session.save(candidate);//保存用户
		session.getTransaction().commit(); //提交事务
		session.close();
	}

	public List<Candidate> listCandidate(){
		Session session = HibernateSessionFactory.getSession();
		session.beginTransaction();//开启事务
		Query query = session.createSQLQuery("select * from Candidate").addEntity(Candidate.class);
		List<Candidate> list = query.list();
		session.getTransaction().commit();//提交事务
		session.close();
		return list;
	}

	public void updateCandidate(Candidate candidate)
	{
		Session session = HibernateSessionFactory.getSession();
		session.beginTransaction();
		session.update(candidate);
		session.getTransaction().commit();
		HibernateSessionFactory.closeSession();
	}

}
